class FullscreenPlayController < ApplicationController
  before_action :exception_stb, only: [:index]

  def index
    # 播放类型 play_type
    # once 只播放单曲, 播放完成后退出
    # bunch 播放一串列表
    # combo  当前选择与 picklist 组成列表播放
    #
    # 推荐{
    #     单个mv  只播放单曲, 播放完成后退出
    #     歌单    点击某首歌曲, 当前首与其他,组成一个播单队列, 进行循环
    # }
    #
    # 排行  {
    #     单个mv  只播放单曲, 播放完成后退出
    #     歌单    点击某首歌曲, 当前首与其他,组成一个播单队列, 进行循环
    # }
    #
    # MV 与已点歌曲组成列表,进行循环
    #
    # 歌手    全屏时候,循环自己的歌曲
    #         从二级页面返回一级页面, 对歌手footer进行焦点选中,
    @items = []
    case params[:play_type]
      when "once"
        if params[:song_id]
          @items << Song.find(params[:song_id])
        elsif params[:code]
          @items << Song.find_by(uuid: params[:code])
        end
      when "playlist_bunch"
        playlist = Playlist.includes(:songs).find(params[:id])
        @items = split_songs(playlist.songs)
      when "singer_bunch"
        singer = Singer.includes(:songs).find(params[:id])
        @items = split_songs(singer.songs)
      when "song_fav_bunch"
        song_fav = current_user.song_fav
        @items = split_songs(song_fav.songs)
      when "picklist_bunch"
        picklist = current_user.picklist
        @items = split_songs(picklist.songs)
      when "history_bunch"
        history = current_user.history
        @items = split_songs(history.songs)
      when "combo"
        song = Song.find(params[:song_id])
        picks = current_user.picklist.songs
        @items << song
        @items += picks
    end
    @fav_ids = current_user.song_fav.song_fav_songs.pluck(:song_id)
    [@items, @fav_ids]
  end

  private

    def split_songs(songs)
      index = songs.find_index(Song.find(params[:song_id]))
      songs[index ...] + songs[0 ... index]
    end
end
